package com.p2p.auth.controller.api.app;

import com.p2p.auth.common.base.JsonResult;
import com.p2p.auth.common.base.ProcessAPI;
import com.p2p.auth.common.enums.ArticleStatus;
import com.p2p.auth.common.enums.ArticleType;
import com.p2p.auth.service.context.ArticleContext;
import com.p2p.auth.controller.Routes;
import com.p2p.auth.controller.api.APIController;
import com.p2p.auth.domain.entity.Article;
import com.p2p.auth.service.repository.ArticleRepository;
import com.p2p.auth.domain.dto.QueryPagingDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author Antinomy
 * @date 2016-11-16
 */

@Api(value = "官网文章模块-前端接口", description = "官网文章模块-前端接口")
@RestController
public class AppArticleController extends APIController {

    private Logger logger = LoggerFactory.getLogger(AppArticleController.class);

    @Autowired
    private ArticleRepository articleRepo;

    @Autowired
    private ArticleContext articleContext;


    @ApiOperation(value = "点击文章详情", notes = "点击文章详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.APP_ARTICLE_DETAIL, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult detail(HttpServletRequest request, @PathVariable String uuid) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("uuid", uuid);

            Article article = articleRepo.findByUuid(uuid);
            article.setClickCounts(article.getClickCounts() + 1L);
            articleRepo.save(article);

            HashMap result = articleContext.renderActicle(article);
            jsonResult.result = result;
        };

        return api.process(request);
    }


    @ApiOperation(value = "文章列表", notes = "文章列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.APP_ARTICLE_TYPE_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(HttpServletRequest request, QueryPagingDTO pagingDto, @PathVariable String type) {

        ProcessAPI api = (jsonResult, validate) -> {
            String typeTxt = validate.checkAndPrepare("type", type);
            validate.check("curPage", pagingDto.getCurPage());
            validate.check("pageSize", pagingDto.getPageSize());

            ArticleType articleTypetype = ArticleType.valueOf(typeTxt);

            Pageable pageable = getPage(pagingDto);
            Page<Article> articles = articleRepo.findByTypeAndStatusOrderByEventDateDesc(articleTypetype, ArticleStatus.PUBLISHED, pageable);
            List result = articleContext.renderActicles(articles);

            jsonResult.result = result;
            jsonResult.size = articles.getTotalElements();
        };

        return api.process(request);
    }
}





